#ifndef EVENTLOOP_TEST_H
#define EVENTLOOP_TEST_H

#include "include/gtest.h"
#include "../Tool/include/EventLoop.h"
#include "../Tool/include/ILogger.h"

using namespace Tool;

TEST(EventLoop, EventLoopTest)
{
    EventLoop loop(2);
    loop.Start();

    bool eventHandled = false;
    loop.RegisterHandler(EventType::Message, [&](std::shared_ptr<Event> event) 
    {
        auto msgEvent = std::dynamic_pointer_cast<MessageEvent>(event);
        if (msgEvent) 
        {
            LOG_INFO("Received message event: id=%d, content=%s", msgEvent->msg_id, msgEvent->content.c_str());
            eventHandled = true;
        }
    });

    auto event = std::make_shared<MessageEvent>(1, "Hello, World!");
    loop.PostEvent(event);


    // 主线程如果不等待，会直接执行后续代码调用 EventLoop析构函数
    // EXPECT_TRUE(eventHandled);
    while (!eventHandled)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(10)); // 等待事件处理
    }
    loop.Stop();
}

TEST(EventLoop, EventLoopUserRole)
{
    struct MyEvent : Event
    {
        std::string str;
        MyEvent(std::string s, EventPriority p = EventPriority::Normal)
        : Event(EventType::UserRole + 1, p)
        , str(s)
        {
        }
    };

    EventLoop loop(2);
    loop.Start();

    bool eventHandled = false;
    loop.RegisterHandler(EventType::UserRole + 1, [&](std::shared_ptr<Event> event)
    {
        auto msgEvent = std::dynamic_pointer_cast<MyEvent>(event);
        if (msgEvent) 
        {
            LOG_INFO("Received message event: str=%s", msgEvent->str.c_str());
            eventHandled = true;
        }
    });

    auto event = std::make_shared<MyEvent>("my MyEvent");
    loop.PostEvent(event);

    while (!eventHandled)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    loop.Stop();
}

TEST(EventLoop, EventLoopRun)
{
    EventLoop loop(2);
    loop.Start();

    loop.RegisterHandler(EventType::Message, [&](std::shared_ptr<Event> event) 
    {
        auto msgEvent = std::dynamic_pointer_cast<MessageEvent>(event);
        if (msgEvent) 
        {
            LOG_INFO("Received message event: id=%d, content=%s", msgEvent->msg_id, msgEvent->content.c_str());
        }
    });

    std::thread producer([&loop]() {
        for (int i = 0; i < 5; ++i) 
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
            auto event = std::make_shared<Tool::MessageEvent>(i, "test message");
            loop.PostEvent(event);
        }
    });

    producer.join();
    std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // 等待事件处理
    loop.Stop();
//    loop.Start();
}

TEST(EventLoop, EventLoopTimer)
{
//    EventLoop loop(1);
//    loop.Start();
//    loop.AddTimer(100,[](){
//        LOG_INFO("定时任务100ms")
//        });

    int _A = 10;
    constexpr int x = 20;
    constexpr int y = x + 2;
//    std::vector<const int> a;

//    std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // 等待事件处理
//    loop.Stop();
}

#endif // CONFIG_TEST_H
